'use strict';
app.factory('HttpUtilService', function($http,$q,APPCONSTANT,Base64Service) {
    function httpPassageway(HttpType,urlString,params,token){
      var deferred = $q.defer();
      //todo:发布的时候这里要修改删除“.json”参数
      var url = APPCONSTANT.pathUrl + urlString+".json";
      //var url = "http://spapi.0101hr.com/index.php/groupbuy/more?version=1.1&items_per_page=5&current_page=1&more=abc";
      var AuthObj={};
      if(token){
        AuthObj = {
          "code": APPCONSTANT.code,
          "version":APPCONSTANT.version,
          "deviceType": APPCONSTANT.deviceType,
          "token":token
        }
      }else{
        AuthObj = {
          "code": APPCONSTANT.code,
          "version":APPCONSTANT.version,
          "deviceType": APPCONSTANT.deviceType
        }
      }
      //var Authorization = BASE64.encoder(JSON.stringify(AuthObj));
      var Authorization = Base64Service.base64encode(JSON.stringify(AuthObj));
      if(HttpType == "post"){
        var transform = function(data){
          return $.param(data);
        }
        $http.post(url,params,{
          headers:{'Authorization':Authorization,'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
          params:params,
          transformRequest: transform
        }).success(function (response) {
            if(response.code == "000000"){
                deferred.resolve(response)
            } else {
                console.log(url+" code:"+ response.code)

            }
        }).error(function (error) {
            deferred.reject(error)
        })
      }else{
        //url+="?"
        //for(var key in params){
        //  url+=key;
        //  url+="=";
        //  url+=params[key];
        //  url+="&";
        //}
        //url +="callback=JSON_CALLBACK";
        //todo:这里还要验证jsonp能不能使用，不行再修改成get方法
        $http.get(url,{
          headers:{'Authorization':Authorization,'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},
          params:params
        }).success(function (response) {
            if(response.code == "000000"){
                deferred.resolve(response)
            } else {
                console.log(url+" code:"+ response.code)
            }
        }).error(function (error) {
          deferred.reject(error)
        })
      }
      return deferred.promise;
    }
    function httpPostNoToken(urlString,params){
      var deferred = $q.defer();
      httpPassageway("post",urlString,params,null).then(function (data) {
        deferred.resolve(data);
      },function (error) {
        deferred.reject(error)
      })
      return deferred.promise;
    }
    function httpPostHasToken (urlString,params,token){
      var deferred = $q.defer();
      httpPassageway("post",urlString,params,token).then(function (data) {
        deferred.resolve(data);
      },function (error) {
        deferred.reject(error)
      })
      return deferred.promise;
    }
    function httpGetNoToken(urlString,params){
      var deferred = $q.defer();
      httpPassageway("get",urlString,params,null).then(function (data) {
        deferred.resolve(data);
      },function (error) {
        deferred.reject(error)
      })
      return deferred.promise;
    }
    function httpGetHasToken(urlString,params,token){
      var deferred = $q.defer();
      httpPassageway("get",urlString,params,token).then(function (data) {
        deferred.resolve(data);
      },function (error) {
        deferred.reject(error)
      })
      return deferred.promise;
    }
    return {
      httpPostNoToken:httpPostNoToken,
      httpPostHasToken:httpPostHasToken,
      httpGetNoToken:httpGetNoToken,
      httpGetHasToken:httpGetHasToken
    };
  })

